﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Xml.XPath;
using TheOutfield.UmbExt.UniversalMediaPicker.Xslt;
using TheOutfield.UmbExt.UniversalMediaPicker.Extensions;
using umbraco;

namespace TheOutfield.UmbExt.UniversalMediaPicker.Providers.YouTube
{
    public class YouTubeXsltExtension : AbstractXsltExtension
    {
        public static XPathNodeIterator GetMedia(int docTypeDefinitionId, string id)
        {
            var service = GetProvider<YouTubeProvider>(docTypeDefinitionId).Service;

            try
            {
                var video = service.GetVideo(id);

                var xd = new XmlDocument();
                var docNode = xmlHelper.addTextNode(xd, "Video", "");

                if (video != null)
                {
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Id", video.Id.ToString()));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Url", video.Url));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Title", video.Title));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "ThumbnailLarge", video.ThumbnailLarge));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "ThumbnailSmall1", video.ThumbnailSmall1));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "ThumbnailSmall2", video.ThumbnailSmall2));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "ThumbnailSmall3", video.ThumbnailSmall3));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Published", video.Published.ToString("yyyy-MM-dd HH:mm:ss")));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Author", video.Author));
                    docNode.AppendChild(xmlHelper.addCDataNode(xd, "EmbedCode", service.GetVideoEmbedCode(id)));
                }

                xd.AppendChild(docNode);

                return xd.CreateNavigator().Select("/Video");
            }
            catch (Exception e)
            {
                return e.ToXPathNodeIterator();
            }
        }

        public static string GetEmbedCode(int docTypeDefinitionId,
            string id,
            int width,
            int height,
            int autohide,
            int autoplay,
            int border,
            int cc_load_policy,
            string color1,
            string color2,
            int controls,
            int disablekb,
            int egm,
            int enablejsapi,
            int fs,
            int hd,
            int iv_load_policy,
            int loop,
            string origin,
            string playerapiid,
            string playlist,
            int rel,
            int showinfo,
            int showsearch,
            int start,
            string wmode)
        {
            var service = GetProvider<YouTubeProvider>(docTypeDefinitionId).Service;
            return service.GetVideoEmbedCode(id,
                width,
                height,
                autohide,
                autoplay,
                border,
                cc_load_policy,
                color1,
                color2,
                controls,
                disablekb,
                egm,
                enablejsapi,
                fs,
                hd,
                iv_load_policy,
                loop,
                origin,
                playerapiid,
                playlist,
                rel,
                showinfo,
                showsearch,
                start,
                wmode);
        }
    }
}